/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.formatter;

import org.eclipse.dltk.formatter.FormatterNodeRewriter;
import org.eclipse.dltk.formatter.IFormatterContainerNode;
import org.eclipse.dltk.formatter.IFormatterDocument;
import org.eclipse.dltk.formatter.IFormatterNode;
import org.eclipse.php.internal.core.parser.PHPParserResult;

/**
 * @author Sean W. Quinn (swquinn@gmail.com)
 *
 */
public class PHPFormatterNodeRewriter extends FormatterNodeRewriter {

  public PHPFormatterNodeRewriter(PHPParserResult result) {
    //    for (Iterator i = result.getCommentNodes().iterator(); i.hasNext();) {
    //      CommentNode commentNode = (CommentNode) i.next();
    //      if (!commentNode.isBlock()) {
    //        addComment(commentNode.getStartOffset(), commentNode.getEndOffset(), commentNode);
    //      }
    //    }
  }

  public void rewrite(IFormatterContainerNode root) {
    mergeTextNodes(root);
    insertComments(root);
    attachComments(root);
  }

  private void attachComments(IFormatterContainerNode root) {
    //    final List commentNodes = new ArrayList();
    //    final List comments = new ArrayList();
    //    final List body = root.getBody();
    //    for (Iterator i = body.iterator(); i.hasNext();) {
    //      IFormatterNode node = (IFormatterNode) i.next();
    //      if (node instanceof FormatterCommentNode) {
    //        comments.add(node);
    //      }
    //      else if (FormatterUtils.isNewLine(node) && !comments.isEmpty()
    //          && comments.get(comments.size() - 1) instanceof FormatterCommentNode) {
    //        comments.add(node);
    //      }
    //      else if (!comments.isEmpty()) {
    //        if (node instanceof IFormatterCommentableNode) {
    //          ((IFormatterCommentableNode) node).insertBefore(comments);
    //          commentNodes.addAll(comments);
    //        }
    //        comments.clear();
    //      }
    //    }
    //    body.removeAll(commentNodes);
    //    for (Iterator i = body.iterator(); i.hasNext();) {
    //      final IFormatterNode node = (IFormatterNode) i.next();
    //      if (node instanceof IFormatterContainerNode) {
    //        attachComments((IFormatterContainerNode) node);
    //      }
    //    }
  }

  @Override
protected IFormatterNode createCommentNode(IFormatterDocument document, int startOffset, int endOffset, Object object) {
    //return new FormatterCommentNode(document, startOffset, endOffset);
    return null;
  }

}
